import AUTH from "../../../utils/auth";
import { getCommissionBalanceByCustId, commissionWithdraw } from "../../../api/commissionBalanceController";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    balanceId:'',   //佣金持仓ID
    detail:{
      availableBalance:0
    },
    inputInfo:{
      withdrawAmount:'',
      wxAccountNo:'',
      accountName:'',
      accountNo:''
    },    //输入内容
    userInfo:'',
    content:{
      modalView: false,
      title: '温馨提示',
      message: '需要完成实名认证，才可以提现！'
    }
  },

  onLoad(){
    this.getCommissionBalanceByCustId()
    this.setData({
      ['inputInfo.wxAccountNo']: wx.getStorageSync('userInfo').nickName,
      ['inputInfo.accountName']: wx.getStorageSync('userInfo').realName
    })
  },
  /**
   * 根据Id查询佣金持仓
   */
  async getCommissionBalanceByCustId(){
    let resolve = await getCommissionBalanceByCustId({
      custId: wx.getStorageSync('custId'),
    })
    if (resolve.data==null) {
      resolve.data={
        availableBalance: 0
      }
    }
    this.setData({
      detail: resolve.data
    })
  },
  /**
   * 赋值
   */
  setInput(e){
    let { type } = e.currentTarget.dataset
    let { value } = e.detail
    console.log(e);
    // withdrawAmount, wxAccountNo, accountName
    this.setData({
      ['inputInfo.'+type]: value,
    })
  },
  /**
   * 提取
   */
  async extract(){
    let realName =  AUTH.checkRealName()
    if (!realName) {
      this.setData({
        ['content.modalView']: true
      })
      return
    }

    if (!this.data.detail.id) {
      wx.showToast({
        title: '没有可提现佣金',
        icon: 'none'
      })
      return
    }
    
    let { withdrawAmount, wxAccountNo, accountName, accountNo } = this.data.inputInfo
    let numberReg = /^[0-9]*$/
    if (wxAccountNo=='' || wxAccountNo==null) {
      wx.showToast({
        title: '请填写微信号',
        icon: 'none'
      })
      return
    }

    if (accountName=='' || accountName==null) {
      wx.showToast({
        title: '请填写提现到账的银行卡户名',
        icon: 'none'
      })
      return
    }
    if (accountNo=='' || accountNo==null) {
      wx.showToast({
        title: '请填写提现到账的银行卡号码',
        icon: 'none'
      })
      return
    }

    if (withdrawAmount=='' || withdrawAmount==null) {
      wx.showToast({
        title: '请填写提现金额',
        icon: 'none'
      })
      return
    }

    if (!numberReg.test(withdrawAmount)) {
      wx.showToast({
        title: '请填写数字',
        icon: 'none'
      })
      return
    }

    if ( this.data.inputInfo.withdrawAmount> this.data.detail.availableBalance) {
      wx.showToast({
        title: '您还没有那么多酒钱~',
        icon: 'none'
      })
      return
    }


    let resolve = await commissionWithdraw({
      commissionBalanceId: this.data.detail.id,
      ...this.data.inputInfo
    })
    if (resolve.success) {
      wx.showToast({
        title: '提现成功！',
        icon: 'success'
      })
      let inputInfo = {
        withdrawAmount:'',
        wxAccountNo:'',
        accountName:''
      }
      this.setData({
        inputInfo:inputInfo,
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 1500);
    }
  },
  /**
   * 弹窗确认
   */
  handlerOk(){
    wx.navigateTo({
      url: '/pages/registerRealName/index',
    })
    this.setData({
      ['content.modalView']: false
    })
  },
    /**
   * 弹窗取消
   */
  handlerNo(){
    this.setData({
      ['content.modalView']: false
    })
  },
})